<?php
defined('_JEXEC') or die('Restricted access');
require_once(JPATH_SITE.DS.'components'.DS.'com_ecommerce'.DS.'models'.DS.'list.php');
if(!isset($_SESSION['cart'])){
	$_SESSION['cart'] = array();
}
?>
<script type="text/javascript" src="templates/bookrengoring/js/jquery.ui.datepicker-da.js"></script>
<script type="text/javascript">
	$(function(){
	
		$(".sliding-div").hide();
		$(".order-btn").show();
		
		$(".order-btn").click(function(){
			$(this).addClass("hidden")
			$(".sliding-div").slideToggle();
		});
		$(".send-order-btn, .cancel-btn").click(function(){
			//$(".order-btn").removeClass("hidden")
			//$(".sliding-div").slideToggle();
			$('#bestilRengoringForm').submit();
		});
		$('#datepicker').datetimepicker({ minDate: 0, showSecond: true, timeFormat: 'hh:mm:ss' }); 
		$('div#st_horizontal').slideTabs();
	});

</script>
<!-- Begin main content -->
<div id="main">
	<div id="content">
		<!-- Begin bestil content  -->
		<div class="content-ctn">
			<div id="bestilContent">
				<div id="bestilNav">
					<ul>
						<li class="active"><a href="index.php?option=com_ecommerce&view=list&Itemid=24"><span class="title">BESTIL Rengøring</span><span class="note">Se vores udvalg af tjenester</span></a></li>
						<li><a href="index.php?option=com_ecommerce&view=orders&Itemid=24"><span class="title">MINE BESTILINGER</span><span class="note">Hvad har jeg tidligere bestilt</span></a></li>
						<li><a href="index.php?option=com_user&view=user&layout=myprofile"><span class="title">Min profil</span><span class="note">se og redigere min profil</span></a></li>
					</ul>
					<?php //echo $this->minicart?>
					<div id="minicart">{module minicart}</div>
				</div>
				<form id="bestilRengoringForm" action="index.php" method="post">
					<fieldset>
						<!-- Horizontal tabs -->
						<div id="st_horizontal" class="st_horizontal">                                                

							<div class="st_tabs_container st_sliding_active">               
								
								<a href="#prev" class="st_prev" title="Forrige rengøring område"></a>
								
								<div style="overflow: hidden;" class="st_slide_container">
								
									<ul class="st_tabs">										
										<?php
											$i=1; 
											foreach($this->listcategory as $key=>$value):
										?>
											<li><a href="#st_content_<?php echo $i?>" rel="tab_<?php echo $i;?>" class="st_tab<?php if($i==1){?> st_first_tab st_tab_active <?php }?>"><?php echo $value->name?></a></li>
										<?php $i++;?>
										<?php endforeach;?>    
									</ul>
								
								</div> <!-- /.st_slide_container -->
								
								<a href="#next" class="st_next" title="Mere rengøring område"></a>                                                                                                
							
							</div> <!-- /.st_tabs_container -->
						   
							<div class="st_view_container">                    
								
								<div class="st_view">                 
									<?php $countk=1;?>                 
									<?php foreach($this->listcategory as $key=>$value):?>		
									<div id="st_content_<?php echo $countk; ?>" class="st_tab_view<?php if($countk==1){ ?> st_first_tab_view st_active_view<?php } ?>">
									<div id="categorycontent<?php echo $value->id; ?>">
										<div class="title-bar">
											<h2>BESTIL STUEPLAN Rengøring</h2>
											<?php
												$model = new EcommerceModelList();
												$result = $model->getProductByCatid($value->id);
												$listproduct = $result["data"];
												$link = $result["link"];
											?>
											<!-- Pager-->
											<div class="navigator-bar fl-right">
												<div id="pagination<?php echo $value->id?>" class="pagination" class="pager fl-right">
													<?php echo $link;?>													
												</div>
												<input type="hidden" name="categoryid<?php echo $value->id?>" id="categoryid<?php echo $value->id?>" value="<?php echo $value->id?>"/>
											</div>
											<!-- /Pager-->
											<div class="sort fl-right">
												<label>Sortering</label>
												<select id="sortOrder<?php echo $value->id?>" onchange="setPaging('<?php echo $value->id?>')">
													<option value="ASC">Diverse A - Z</option>
													<option value="DESC">Diverse Z - A</option>
												</select>
												<label>Visning</label>
												<select id="viewNum<?php echo $value->id?>" name="viewNum" class="viewNum" onchange="setPaging('<?php echo $value->id?>')">													
													<option value="10" selected="selected">10</option>
													<option value="20">20</option>
													<option value="30">30</option>
													<option value="0">Alle</option>
												</select>
											</div>
										</div>
										<div class="table-list" id="table-list<?php echo $value->id?>">
											<table class="order" cellpadding="0" cellspacing="0">
												<tbody>
													<tr class="first">
														<th width="450" class="left">Navn område</th>
														<th width="60">Kvalitet</th>
														<th>Vælg</th>
														<th width="148" class="right">Pris (DKK)</th>
													</tr>
													<?php
														$j=1;
														if(count($listproduct)){
														foreach($listproduct as $k=>$v):	
													?>
													<tr <?php if($j%2==0){ ?>class="odd"<?php }else{?>class="even"<?php }?>>
													  <td class="area-name left"><?php echo $v->name?></td>
													  <td>
														<select class="quality-selector" id="quality<?php echo $v->id;?><?php echo $value->id?>" onchange="getPrice(this,'<?php echo $v->id?>','<?php echo $value->id?>')" onclick="checkactions('<?php echo $v->id?>','<?php echo $value->id?>');">
															<?php
																foreach($this->producttype as $prot=>$type):?>
																<?php 
																$arr_search=array("category_id"=>$value->id,"product_id"=>$v->id,"producttype_id"=>$type->id);
																$typeselected = "1";																
																?>
																<?php if(in_array($arr_search, $_SESSION['cart'])){?>
																<?php 	$typeselected = $type->id;?>
																<option value="<?php echo $type->id?>" selected="selected"><?php echo $type->name ?></option>
																<?php }else{?>
																<option value="<?php echo $type->id?>"><?php echo $type->name ?></option>
																<?php }?>
															<?php 
																endforeach;
															?>
														</select>														
													  </td>
														<td class="actions">
														<?php $arr_search=array("category_id"=>$value->id,"product_id"=>$v->id,"producttype_id"=>$typeselected);?>
														<?php if(in_array($arr_search, $_SESSION['cart'])){?>
														<input class="check-box" type="checkbox" value="1" id="actions<?php echo $v->id?><?php echo $value->id?>" onclick="updateproduct('<?php echo $v->id?>','<?php echo $value->id?>')" checked="checked" />
														<?php }else{?>
														<input class="check-box" type="checkbox" value="1" id="actions<?php echo $v->id?><?php echo $value->id?>" onclick="updateproduct('<?php echo $v->id?>','<?php echo $value->id?>')" />
														<?php }?>
														</td>
															
														<td width="148" class="price right">
														<span id="productprice<?php echo $v->id?><?php echo $value->id?>">
														<?php 
															$price = $model->getPriceById($value->id, $v->id, $typeselected);
															echo $this->formatPrice($price);
														?>
														</span>
														</td>
													</tr>
													<?php 
														$j++;
														endforeach;
														}
													?>
													<tr class="bottom">
														<td class="right" colspan="3">Subtotal på kategori "Stueplan" :</td>
														<td class="sub-total right"><span id="categorytotalprice<?php echo $value->id?>"><?php echo $model->gettotalPriceByCatid($value->id);?></span></td>
													</tr>
												</tbody>
											 </table>
										</div>                            
									</div>
									</div>
									<?php $countk++;?>
                                    <?php endforeach;?>
								</div> <!-- /.st_view -->
							 
							</div> <!-- /.st_view_container -->                                          
						
						</div>         
						<!-- End Horizontal tabs -->
						<div class="order-total">
							<span class="total">Total</span>
							<span class="amount" id="totalprice"><?php echo $model->gettotalPrice();?></span>
						</div>
						

						<a class="order-btn" href="javascript:void(0)">Ordre Nu</a>
						<div id="orderForm" class="sliding-div" style="display:none;">
							<h2>Ordre formular</h2>
							<div class="form-ctn">
								<div class="client-info">
									<h3>Kunde oplysninger</h3>
									<?php //var_dump($this->userinfo)?>
									<span class="label">Firmanavn :</span>
									<span><?php echo $this->userinfo->company;?></span>
									<span class="label">Kontaktperson :</span>
									<span><?php echo $this->userinfo->name;?></span>
									<span class="label">Adresse :</span>
									<span><?php echo $this->userinfo->address;?></span>
									<span class="label">Postnr.:</span>
									<span><?php echo $this->userinfo->post;?></span>
									<span class="label">Bynavn :</span>
									<span><?php echo $this->userinfo->city;?></span>
									<span class="label">Tlf.:</span>
									<span><?php echo $this->userinfo->phonetable;?></span>
									<span class="label">Mobil :</span>
									<span><?php echo $this->userinfo->mobil;?></span>
									<span class="label">Email :</span>
									<span><?php echo $this->userinfo->email;?></span>
									<span class="label">CVR-nr. :</span>
									<span><?php echo $this->userinfo->post;?></span>
								</div>
								<div class="order-info">
									<h3>Bestiling oplysninger</h3>
									<label>Kommentar :</label>
									<textarea cols="20" rows=" 10" name="comment" id="comment" ></textarea>
									<label>Udførelses dato<span>*</span> </label>
									<input id="datepicker" class="input-txt" type="text" value="" name="finishDate" />
									<label class="check-box"><input class="check-box" type="checkbox" name="sendorder" checked="checked" />Send ordre copy til din E-Mail</label>
									<p class="note">Felt markeret med <span>*</span> er påkrævet.  </p>
									<div class="form-submit">
										<a class="send-order-btn" href="javascript:void(0)">Send Ordre</a>
										<!--a class="cancel-btn" href="javascript:void(0)">Annualler</a-->
									</div>
									
								</div>
							</div>
						</div>
					</fieldset>
					<input type="hidden" name="option" value="com_ecommerce" />
				    <input type="hidden" name="view" value="list" />
				    <input type="hidden" name="task" value="checkOut" />
				    <input type="hidden" name="name" value="<?php echo $this->userinfo->name;?>" />
				    <input type="hidden" name="email" value="<?php echo $this->userinfo->email;?>" />
				    <input type="hidden" name="firmname" value="<?php echo $this->userinfo->company;?>" />
				    <input type="hidden" name="telephone" value="<?php echo $this->userinfo->phonetable;?>" />
				    <input type="hidden" name="Itemid" value="<?php echo JRequest::getVar('Itemid', 0, 'get'); ?>" />
				    <?php echo JHTML::_( 'form.token' ); ?>
				</form>
				
			</div>
		</div>
		<!-- / bestil content  -->
		
	</div>
	<!-- / main-content  -->
	  
	<!-- /end mid-content  -->
</div>
<!-- / end main content -->

<script type="text/javascript">
	function getMinicart(){
		 $.ajax
			({
				type:"POST",
				url:"index.php?option=com_ecommerce&task=getminicart&tmpl=component",
				data:"rand="+Math.random(),
				success:function(result)
				{
				   $("#minicart").html(result);
				}
			});	
	}

	function getPrice(obj,product_id,category_id){
		 var quality = $(obj).val();
		 $.ajax
			({
				type:"POST",
				url:"index.php?option=com_ecommerce&task=getprice&tmpl=component",
				data:"category_id="+category_id+"&product_id="+product_id+"&quality="+quality+"&rand="+Math.random(),
				success:function(result)
				{
				   $("#productprice"+product_id+category_id).html(result);
				}
			});	
	}

	function updateproduct(product_id,category_id){								
		var actions = $('#actions'+product_id+category_id).is(':checked')==true?1:0;
		var quality = $("#quality"+product_id+category_id).val();
		$.ajax
		({
			type:"POST",
			url:"index.php?option=com_ecommerce&task=getcart&tmpl=component",
			data:"product_id="+product_id+"&quality="+quality+"&category_id="+category_id+"&actions="+actions+"&rand="+Math.random(),
			success:function(result)
			{
			   $("#categorytotalprice"+category_id).html(result);
			   gettotalprice();
			   getMinicart();
			}
		});
	}

	function gettotalprice(){
		$.ajax
		({
			type:"POST",
			url:"index.php?option=com_ecommerce&task=gettotalprice&tmpl=component"+"&rand="+Math.random(),									
			success:function(result)
			{
			   $("#totalprice").html(result);
			}
		});
	}

	function checkactions(product_id,category_id){
		var actions = $('#actions'+product_id+category_id).is(':checked')==true?1:0;
		if(actions==1){
			$('#actions'+product_id+category_id).attr("checked",false);
			updateproduct(product_id,category_id);
		}
	}
	
	
	function setPaging(category_id){
		var start = 0;
		var sortOrder = $('#sortOrder'+category_id).val();
		var viewNum = $('#viewNum'+category_id).val();
		$.ajax
		({
			type:"POST",
			url:"index.php?option=com_ecommerce&task=paging&no_html=1",
			data:"limitstart="+start+"&category_id="+category_id+"&orders="+sortOrder+"&limit="+viewNum+"&rand="+Math.random(),
			success:function(result)
			{
			   $("#categorycontent"+category_id).html(result);
			}
		});
	}
	
	function paging(obj,start){
		var curr = $(obj);
		var category_id = curr.parent("div").parent("div.navigator-bar").find("input").val();
		var sortOrder = $('#sortOrder'+category_id).val();
		var viewNum = $('#viewNum'+category_id).val();
		$.ajax
		({
			type:"POST",
			url:"index.php?option=com_ecommerce&task=paging&no_html=1",
			data:"limitstart="+start+"&category_id="+category_id+"&orders="+sortOrder+"&limit="+viewNum+"&rand="+Math.random(),
			success:function(result)
			{
			   $("#categorycontent"+category_id).html(result);
			}
		});
	}
</script>